package action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ModelDriven;

import dao.Message;
import dao.MessageDAO;
import dao.Users;
import dao.pageDAO;

public class MessageAction implements ModelDriven<Message> {

	private Message message;
	private HttpServletRequest request = ServletActionContext.getRequest();
	private HttpServletResponse response = ServletActionContext.getResponse();
	public void switchForLogin() throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String submittype = (String) request.getParameter("submittype");
		switch(submittype) {
		case "返回查看所有留言": request.getSession().removeAttribute("MessageType");
						request.getSession().removeAttribute("MessageName");break;
		case "游客查看留言": request.getSession().setAttribute("MessageType","youke");break;
		case "查询本人留言": request.getSession().setAttribute("MessageType","benren");break;
		case "查询指定用户留言": request.getSession().setAttribute("MessageType","zhiding");
						String messagename=(String)request.getParameter("name");
						request.getSession().setAttribute("MessageName",messagename);break;
		default:break;
		}
	}
	
	public String getMessage() throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		switchForLogin();
		String MessageType=(String) request.getSession().getAttribute("MessageType");
		String name =(String)request.getSession().getAttribute("MessageName");
		Users obj=(Users)request.getSession().getAttribute("UserMessage");
		String name1 = null;
		if(obj!=null){
			 name1=obj.getUsername();
		}
		MessageDAO messageDao = new MessageDAO();
		pageDAO page= new pageDAO();
		page.setPageSize(4);
		String pn=request.getParameter("pageNumber");
		if(pn==null) {
			page.setPageNumber(1);
		}else {
			page.setPageNumber(Integer.parseInt(pn));
		}
		if("benren".equals(MessageType)){
			page = messageDao.getAll(name1,page);
		}else if("zhiding".equals(MessageType)){
			page = messageDao.getAll(name,page);
		}else{
			page = messageDao.getAll(page);
		}
		request.setAttribute("page", page);
		return "success";
	}
	
	public String insert() throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		Users obj=(Users)request.getSession().getAttribute("UserMessage");
		String name1 =obj.getUsername();
		String name2 =obj.getNickname();
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time=sdf.format(new Date());
		boolean u=new MessageDAO().insert(name1,name2, title,content,time);
		if (u == true) {
			return "success";
		}
		else {
			return "error";
		}
	}

	public String update() throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		switchForLogin();
		String MessageType=(String) request.getSession().getAttribute("MessageType");
		String id=(String)request.getParameter("id");
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time=sdf.format(new Date());
		boolean u = new MessageDAO().update(id,title,content,time);
			if (u == true) {
				if("benren".equals(MessageType)||"zhiding".equals(MessageType)) {
					return "success";
				}else {
					return "success";
				}
			}
			else {
				request.setAttribute("error", "修改不成功！");
				return "error";
			}
	}

	public String delete() throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		switchForLogin();
		String MessageType=(String) request.getSession().getAttribute("MessageType");
		String id=request.getParameter("id");
		boolean u=new MessageDAO().delete(id);
		if (u ==true) {
			if("benren".equals(MessageType)||"zhiding".equals(MessageType)) {
				request.setAttribute("error", "已删除！");
				return "success";
			}else {
				request.setAttribute("error", "已删除！");
				return "error";
			}
		}
		else {
			request.setAttribute("error", "删除失败！");
			return "error";
		}
	}
	@Override
	public Message getModel() {
		// TODO Auto-generated method stub
		message=new Message();
		return this.message;
	}

}
